package com.cyanite.playlv2.util;

import com.cyanite.playlv2.PlayLv2;
import com.cyanite.playlv2api.PlayLv2Api;
import com.cyanite.playlv2api.types.User;
import com.cyanite.playlv2.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import java.io.IOException;
import java.util.Observable;

public class UserUtils {

    public static void ensureUserPhoto(final Context context, final User user, final boolean DEBUG,
            final String TAG) {
        Activity activity = ((Activity) context);
        final ImageView photo = (ImageView) activity.findViewById(R.id.photo);
        if (user.getPhoto() == null) {
            photo.setImageResource(R.drawable.blank_boy);
            return;
        }
        final Uri photoUri = Uri.parse(user.getPhoto());
        if (photoUri != null) {
            RemoteResourceManager userPhotosManager = ((PlayLv2) activity.getApplication())
                    .getRemoteResourceManager();
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(userPhotosManager
                        .getInputStream(photoUri));
                photo.setImageBitmap(bitmap);
            } catch (IOException e) {
                userPhotosManager.addObserver(new RemoteResourceManager.ResourceRequestObserver(
                        photoUri) {
                    @Override
                    public void requestReceived(Observable observable, Uri uri) {
                        observable.deleteObserver(this);
                        updateUserPhoto(context, photo, uri, user, DEBUG, TAG);
                    }
                });
                userPhotosManager.request(photoUri);
            }
        }
    }

    private static void updateUserPhoto(Context context, final ImageView photo, final Uri uri,
            final User user, final boolean DEBUG, final String TAG) {
        final Activity activity = ((Activity) context);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    RemoteResourceManager userPhotosManager = ((PlayLv2) activity
                            .getApplication()).getRemoteResourceManager();
                    Bitmap bitmap = BitmapFactory.decodeStream(userPhotosManager
                            .getInputStream(uri));
                    photo.setImageBitmap(bitmap);
                } catch (IOException e) {
                    if (PlayLv2Api.MALE.equals(user.getGender())) {
                        photo.setImageResource(R.drawable.blank_boy);
                    } else {
                        photo.setImageResource(R.drawable.blank_girl);
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    public static boolean isFriend(User user) {
        if (user == null) {
            return false;
        } else if (TextUtils.isEmpty(user.getFriendstatus())) {
            return false;
        } else if (user.getFriendstatus().equals("friend")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isFollower(User user) {
        if (user == null) {
            return false;
        } else if (TextUtils.isEmpty(user.getFriendstatus())) {
            return false;
        } else if (user.getFriendstatus().equals("pendingyou")) {
            return true;
        } else {
            return false;
        }
    }

    // pending friends request by you
    public static boolean isFriendStatusPendingYou(User user) {
        return user != null && user.getFriendstatus() != null &&
            user.getFriendstatus().equals("pendingyou");
    }

    // pending friends request by them
    public static boolean isFriendStatusPendingThem(User user) {
        return user != null && user.getFriendstatus() != null &&
        user.getFriendstatus().equals("pendingthem");
    }

    public static boolean isFriendStatusFollowingThem(User user) {
        return user != null && user.getFriendstatus() != null &&
        user.getFriendstatus().equals("followingthem");
    }

    public static int getDrawableForMeTabByGender(String gender) {
        if (gender != null && gender.equals("female")) {
            return R.drawable.tab_main_nav_me_girl_selector;
        } else {
            return R.drawable.tab_main_nav_me_boy_selector;
        }
    }

    public static int getDrawableForMeMenuItemByGender(String gender) {
        if (gender == null) {
            return R.drawable.ic_menu_myinfo_boy;
        } else if (gender.equals("female")) {
            return R.drawable.ic_menu_myinfo_girl;
        } else {
            return R.drawable.ic_menu_myinfo_boy;
        }
    }

    public static int getDrawableByGenderForUserThumbnail(User user) {
    	String gender = user.getGender();
    	if (gender != null && gender.equals("female")) {
            return R.drawable.blank_girl;
        } else {
            return R.drawable.blank_boy;
        }
    }
}
